package com.sfy.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.sfy.auth.application.convert.AuthPermissionDTOConverter;
import com.sfy.auth.application.dto.AuthPermissionDTO;
import com.sfy.auth.common.entity.Result;
import com.sfy.auth.domain.entity.AuthPermissionBO;
import com.sfy.auth.domain.service.AuthPermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/permission/")
@Slf4j
public class PermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * 新增权限
     *
     * @param authPermissionDTO
     * @return
     */
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController:add:{}", JSON.toJSONString(authPermissionDTO));
            }
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.
                    INSTANCE.authPermissionDTOToAuthPermissionBO(authPermissionDTO);
            Boolean add = authPermissionDomainService.add(authPermissionBO);
            return Result.OK(add);
        } catch (Exception e) {
            return Result.Fail();
        }
    }


    /**
     * 更新权限
     *
     * @param authPermissionDTO
     * @return
     */
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController:update:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限ID不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.
                    INSTANCE.authPermissionDTOToAuthPermissionBO(authPermissionDTO);
            Boolean update = authPermissionDomainService.update(authPermissionBO);
            return Result.OK(update);
        } catch (Exception e) {
            return Result.Fail();
        }
    }

    /**
     * 删除权限
     *
     * @param authPermissionDTO
     * @return
     */
    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController:delete:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限ID不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.
                    INSTANCE.authPermissionDTOToAuthPermissionBO(authPermissionDTO);
            Boolean delete = authPermissionDomainService.delete(authPermissionBO);
            return Result.OK(delete);
        } catch (Exception e) {
            return Result.Fail();
        }
    }

}
